<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="第三方编号" prop="supplierCode">
        <el-input
          v-model="queryParams.supplierCode"
          placeholder="请输入第三方编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="第三方名称" prop="supplierName">
        <el-input
          v-model="queryParams.supplierName"
          placeholder="请输入第三方名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="简称" prop="abbreviation">
        <el-input
          v-model="queryParams.abbreviation"
          placeholder="请输入简称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入联系人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="联系人手机号" prop="contactPersonPhone">
        <el-input
          v-model="queryParams.contactPersonPhone"
          placeholder="请输入联系人手机号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="电子邮箱" prop="mail">
        <el-input
          v-model="queryParams.mail"
          placeholder="请输入电子邮箱"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="主体性质" prop="mainNature">
        <el-input
          v-model="queryParams.mainNature"
          placeholder="请输入主体性质"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="第三方类型" prop="supplierType">
        <el-select
          v-model="queryParams.supplierType"
          placeholder="请选择第三方类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="行业类型" prop="industryType">
        <el-select
          v-model="queryParams.industryType"
          placeholder="请选择行业类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="物流属性" prop="logisticsAttr">
        <el-input
          v-model="queryParams.logisticsAttr"
          placeholder="请输入物流属性"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="纳税人类型" prop="taxpayerType">
        <el-select
          v-model="queryParams.taxpayerType"
          placeholder="请选择纳税人类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="纳税人识别号" prop="taxpayerCode">
        <el-input
          v-model="queryParams.taxpayerCode"
          placeholder="请输入纳税人识别号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="开户行名称" prop="accountBankName">
        <el-input
          v-model="queryParams.accountBankName"
          placeholder="请输入开户行名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="开户行账号" prop="accountCode">
        <el-input
          v-model="queryParams.accountCode"
          placeholder="请输入开户行账号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="所在省份" prop="province">
        <el-input
          v-model="queryParams.province"
          placeholder="请输入所在省份"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="所在城市" prop="city">
        <el-input
          v-model="queryParams.city"
          placeholder="请输入所在城市"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="所在区域" prop="region">
        <el-input
          v-model="queryParams.region"
          placeholder="请输入所在区域"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="服务区域" prop="serviceArea">
        <el-input
          v-model="queryParams.serviceArea"
          placeholder="请输入服务区域"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="服务品类" prop="serviceCategory">
        <el-input
          v-model="queryParams.serviceCategory"
          placeholder="请输入服务品类"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="启停状态" prop="disabled">
        <el-select
          v-model="queryParams.disabled"
          placeholder="请选择启停状态"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['mdm:supplier:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['mdm:supplier:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="第三方编号" align="center" prop="supplierCode" />
      <el-table-column label="第三方名称" align="center" prop="supplierName" />
      <el-table-column label="简称" align="center" prop="abbreviation" />
      <el-table-column label="联系人" align="center" prop="contactPerson" />
      <el-table-column label="联系人手机号" align="center" prop="contactPersonPhone" />
      <el-table-column label="电子邮箱" align="center" prop="mail" />
      <el-table-column label="主体性质" align="center" prop="mainNature" />
      <el-table-column label="第三方类型" align="center" prop="supplierType" />
      <el-table-column label="行业类型" align="center" prop="industryType" />
      <el-table-column label="物流属性" align="center" prop="logisticsAttr" />
      <el-table-column label="纳税人类型" align="center" prop="taxpayerType" />
      <el-table-column label="纳税人识别号" align="center" prop="taxpayerCode" />
      <el-table-column label="开户行名称" align="center" prop="accountBankName" />
      <el-table-column label="开户行账号" align="center" prop="accountCode" />
      <el-table-column label="所在省份" align="center" prop="province" />
      <el-table-column label="所在城市" align="center" prop="city" />
      <el-table-column label="所在区域" align="center" prop="region" />
      <el-table-column label="服务区域" align="center" prop="serviceArea" />
      <el-table-column label="服务品类" align="center" prop="serviceCategory" />
      <el-table-column label="启停状态" align="center" prop="disabled" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['mdm:supplier:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['mdm:supplier:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SupplierForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { SupplierApi, SupplierVO } from '@/api/mdm/supplier'
import SupplierForm from './SupplierForm.vue'

/** 第三方信息 列表 */
defineOptions({ name: 'Supplier' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<SupplierVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  supplierCode: undefined,
  supplierName: undefined,
  abbreviation: undefined,
  contactPerson: undefined,
  contactPersonPhone: undefined,
  mail: undefined,
  mainNature: undefined,
  supplierType: undefined,
  industryType: undefined,
  logisticsAttr: undefined,
  taxpayerType: undefined,
  taxpayerCode: undefined,
  accountBankName: undefined,
  accountCode: undefined,
  province: undefined,
  city: undefined,
  region: undefined,
  serviceArea: undefined,
  serviceCategory: undefined,
  disabled: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SupplierApi.getSupplierPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SupplierApi.deleteSupplier(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SupplierApi.exportSupplier(queryParams)
    download.excel(data, '第三方信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>